package hn.cch.druid.repository;

import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.repository.core.RepositoryMetadata;

import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 * @author CCH
 * @version 2018.09.18
 */
public class DruidRepositoryFactory<T, ID extends Serializable>
        extends JpaRepositoryFactory {

    private EntityManager entityManager;

    public DruidRepositoryFactory(EntityManager entityManager) {
        super(entityManager);
        this.entityManager = entityManager;
    }

    protected DruidRepository getTargetRepository(RepositoryMetadata
                                                         repositoryMetadata) {
        JpaEntityInformation<T, Serializable> jpaEnityInformation =
                (JpaEntityInformation<T, Serializable>)
                        getEntityInformation(repositoryMetadata
                                .getDomainType());
        return new DruidRepositoryImpl<T, ID>(jpaEnityInformation,
                entityManager);
    }

    protected Class<?> getRepositoryBaseClass(RepositoryMetadata repositoryMetadata) {
        return DruidRepositoryImpl.class;
    }
}